package com.ken.android.music.view.fragment;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.ken.android.music.db.MusicDao;
import com.ken.android.music.model.MusicBean;
import com.ken.android.music.service.PlayerService;
import com.ken.android.music.R;

import java.util.Observable;
import java.util.Observer;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

/**
 * Created by axnshy on 16/7/31.
 */
public class AlbumDetailFragment extends Fragment implements Observer {
    View view;
    private ImageView musicCover;
    private TextView musicTitle;
    private TextView musicArtist;
    MusicBean mMusicBean;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.artwork, container);
        initView();
        Bundle bundle=getArguments();
        mMusicBean =bundle.getParcelable("MusicInfo");
        return view;
    }

    private void initView() {
        musicCover = (ImageView) view.findViewById(R.id.iv_current_musicAlbumCover);
        musicArtist = (TextView) view.findViewById(R.id.tv_current_musicArtist);
        musicTitle = (TextView) view.findViewById(R.id.tv_current_musicName);
    }

    @Override
    public void update(Observable observable, Object o) {
//        musicTitle.setText(mService.currentMusic.getMusicName());
//        musicArtist.setText(mService.currentMusic.getMusicArtist());
        new Thread(new Runnable() {
            @Override
            public void run() {
                Bitmap bitmap = null;
//                String albumArt = MusicDao.getAlbumArt(view.getContext(), mMusicBean.albumId);
//                Log.e("TAG","albumArt-----------------------"+albumArt);
                Message msg = Message.obtain();
//                msg.obj = albumArt;
                handler.sendMessage(msg);
            }
        }).start();
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            Bitmap bm = BitmapFactory.decodeFile((String) msg.obj);
            BitmapDrawable bmpDraw = new BitmapDrawable(bm);
            musicCover.setImageDrawable(bmpDraw);
        }
    };
}
